//
//  MainViewController.swift
//  Super_WeiBo
//
//  Created by 梁超 on 16/3/15.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        
        super.viewDidLoad()
        // 添加子控制器
        addChildViewControllers()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        // 设置tabBar中间的加号按钮
        setComposeBtn()
        
    }
    
    private func setComposeBtn()
    {
        tabBar.addSubview(composeBtn)
        let with = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: with, height: 49)
        composeBtn.frame = CGRectOffset(rect, with * 2, 0)
        
    }
    
    /**
     添加子控制器
     */
    private func addChildViewControllers()
    {
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        if let jsonPath = path
        {
            let jsonData = NSData(contentsOfFile: jsonPath)
            do{
                // 通过JSON数据获得类名
                let dicArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                // 实例化页面
                for dic in dicArr as! [[String : String]]
                {
                    addChildViewController(dic["vcName"]!, title : dic["title"]!, imgName: dic["imageName"]!)
                }
        
            }catch{
            
                addChildViewController("HomeTableViewController", title: "首页", imgName: "tabbar_home")
                addChildViewController("MessageTableViewController", title: "消息", imgName: "tabbar_message_center")
                addChildViewController("NullViewController", title: "", imgName: "")
                addChildViewController("DiscoverTableViewController", title: "广场", imgName: "tabbar_discover")
                addChildViewController("ProfileTableViewController", title: "我", imgName: "tabbar_profile")
            }
        }
    }
    
    /**
     初始化子控制器
     - parameter childControllerName: 类名
     - parameter title:               标题
     - parameter imgName:             图片名称
     */
    private func addChildViewController(childControllerName: String , title : String , imgName : String)
    {
        // 拿到命名空间
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 转化为类名
        let cls : AnyObject? = NSClassFromString(ns + "." + childControllerName)
        // 实例化类
        let vcCls  = cls as! UIViewController.Type
        
        let vc = vcCls.init()
        
        
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imgName)
        vc.tabBarItem.selectedImage = UIImage(named: imgName + "_highlighted")
        vc.tabBarItem.title = title
        
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        addChildViewController(nav)
        
    }
    /**
     监听发送微博按钮
     */
    func pressComposeBtn()
    {
        let composeVC = ComposeViewContorller()
        let nav = UINavigationController(rootViewController: composeVC)
        presentViewController(nav, animated: true, completion: nil)
    }
    // MARK: - 懒加载
    private lazy var composeBtn : UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "pressComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()

}
